package com.goscom.app.deeset;

import java.io.File;
import java.util.concurrent.ExecutionException;

import com.goscom.app.database.DBAdapter;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class ExportData extends AsyncTask<String, Integer, String> {

	private Context mCtx;
	private String mStrMerId;
	private DBAdapter mDB;
	private ProgressDialog mProgress;

	public ExportData(Context ctx, String strMerId, DBAdapter mDB) {
		this.mCtx = ctx;
		this.mStrMerId = strMerId;
		this.mDB = mDB;
		mProgress = new ProgressDialog(ctx);
	}

	@Override
	protected String doInBackground(String... params) {
		StringBuffer strResult = new StringBuffer();
		ContentValues content = mDB.queryDataByMerId("MER", mStrMerId);
		content = mDB.queryUserById(content.getAsString("userid"));
		String strUsername = content.getAsString("username");
		Log.i("Username", strUsername);

		CollectData collectData = new CollectData(mCtx, strUsername, mStrMerId,
				mDB);
		collectData.execute(mStrMerId);
		try {
			if (collectData.get()) {
				mDB.close();
				String strFileData = "/mnt/sdcard/DCMI/" + mStrMerId + "-merchandiser.txt";
				File fileData = new File(strFileData);
				UploadFile uploadData = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileData.exists()) {
					uploadData.execute(strFileData);
					Log.i("Upload Data", "Successful!");
				}

				String strAttchUpload1 = "/mnt/sdcard/DCMI/" + mStrMerId
						+ "-colleague1.jpg";
				File fileUpload1 = new File(strAttchUpload1);
				UploadFile upload1 = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileUpload1.exists()) {
					upload1.execute(strAttchUpload1);
					Log.i("Upload 1", "Successful!");
				}

				String strAttchUpload2 = "/mnt/sdcard/DCMI/" + mStrMerId
						+ "-colleague2.jpg";
				File fileUpload2 = new File(strAttchUpload2);
				UploadFile upload2 = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileUpload2.exists()) {
					upload2.execute(strAttchUpload2);
					Log.i("Upload 2", "Successful!");
				}

				String strAttchUpload3 = "/mnt/sdcard/DCMI/" + mStrMerId
						+ "-colleague3.jpg";
				File fileUpload3 = new File(strAttchUpload3);
				UploadFile upload3 = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileUpload3.exists()) {
					upload3.execute(strAttchUpload3);
					Log.i("Upload 3", "Successful!");
				}

				String strAttchUpload4 = "/mnt/sdcard/DCMI/" + mStrMerId
						+ "-colleague4.jpg";
				File fileUpload4 = new File(strAttchUpload4);
				UploadFile upload4 = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileUpload4.exists()) {
					upload4.execute(strAttchUpload4);
					Log.i("Upload 4", "Successful!");
				}

				String strAttchSign = "/mnt/sdcard/DCMI/" + mStrMerId
						+ "-signature.png";
				File fileSign = new File(strAttchSign);
				UploadFile uploadSign = new UploadFile(mCtx, strUsername,
						mStrMerId);
				if (fileSign.exists()) {
					uploadSign.execute(strAttchSign);
					Log.i("Upload Sign", "Successful!");
				}

				if (fileData.exists()) {
					if (!uploadData.get()) {
						strResult.append("File data");
					}
				}
				if (fileUpload1.exists()) {
					if (!upload1.get()) {
						if (strResult.toString().equals("")) {
							strResult.append("File colleague1");
						} else {
							strResult.append(", colleague1");
						}
					}
				}
				if (fileUpload2.exists()) {
					if (!upload2.get()) {
						if (strResult.toString().equals("")) {
							strResult.append("File colleague2");
						} else {
							strResult.append(", colleague2");
						}
					}
				}
				if (fileUpload3.exists()) {
					if (!upload3.get()) {
						if (strResult.toString().equals("")) {
							strResult.append("File colleague3");
						} else {
							strResult.append(", colleague3");
						}
					}
				}
				if (fileUpload4.exists()) {
					if (!upload4.get()) {
						if (strResult.toString().equals("")) {
							strResult.append("File colleague4");
						} else {
							strResult.append(", colleague4");
						}
					}
				}
				if (fileSign.exists()) {
					if (!uploadSign.get()) {
						if (strResult.toString().equals("")) {
							strResult.append("File signature");
						} else {
							strResult.append(", signature");
						}
					}
				}
				
				
//				String strFileData = "/mnt/sdcard/DCMI/" + mStrMerId + "-merchandiser.txt";
//				File fileData = new File(strFileData);
//				UploadFile uploadData = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				String strAttchUpload1 = "/mnt/sdcard/DCMI/" + mStrMerId
//						+ "-colleague1.jpg";
//				File fileUpload1 = new File(strAttchUpload1);
//				UploadFile upload1 = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				String strAttchUpload2 = "/mnt/sdcard/DCMI/" + mStrMerId
//						+ "-colleague2.jpg";
//				File fileUpload2 = new File(strAttchUpload2);
//				UploadFile upload2 = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				String strAttchUpload3 = "/mnt/sdcard/DCMI/" + mStrMerId
//						+ "-colleague3.jpg";
//				File fileUpload3 = new File(strAttchUpload3);
//				UploadFile upload3 = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				String strAttchUpload4 = "/mnt/sdcard/DCMI/" + mStrMerId
//						+ "-colleague4.jpg";
//				File fileUpload4 = new File(strAttchUpload4);
//				UploadFile upload4 = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				String strAttchSign = "/mnt/sdcard/DCMI/" + mStrMerId
//						+ "-signature.png";
//				File fileSign = new File(strAttchSign);
//				UploadFile uploadSign = new UploadFile(mCtx, strUsername,
//						mStrMerId);
//				
//				if (fileData.exists()) {
//					uploadData.execute(strFileData);
//					if (!uploadData.get()) {
//						strResult.append("File data");
//					} else {
//						Log.i("Upload data", "Successful!");
//					}
//				}
//				
//				if (fileUpload1.exists()) {
//					upload1.execute(strAttchUpload1);
//					if (!upload1.get()) {
//						if (strResult.toString().equals("")) {
//							strResult.append("File colleague1");
//						} else {
//							strResult.append(", colleague1");
//						}
//					} else {
//						Log.i("Upload 1", "Successful!");
//					}
//					
//				}
//
//				
//				if (fileUpload2.exists()) {
//					upload2.execute(strAttchUpload2);
//					if (!upload2.get()) {
//						if (strResult.toString().equals("")) {
//							strResult.append("File colleague2");
//						} else {
//							strResult.append(", colleague2");
//						}
//					} else {
//						Log.i("Upload 2", "Successful!");
//					}
//				}
//
//				
//				if (fileUpload3.exists()) {
//					upload3.execute(strAttchUpload3);
//					if (!upload3.get()) {
//						if (strResult.toString().equals("")) {
//							strResult.append("File colleague3");
//						} else {
//							strResult.append(", colleague3");
//						}
//					} else {
//						Log.i("Upload 3", "Successful!");
//					}
//				}
//
//				
//				if (fileUpload4.exists()) {
//					upload4.execute(strAttchUpload4);
//					if (!upload4.get()) {
//						if (strResult.toString().equals("")) {
//							strResult.append("File colleague4");
//						} else {
//							strResult.append(", colleague4");
//						}
//					} else {
//						Log.i("Upload 4", "Successful!");
//					}
//				}
//
//				
//				if (fileSign.exists()) {
//					uploadSign.execute(strAttchSign);
//					if (!uploadSign.get()) {
//						if (strResult.toString().equals("")) {
//							strResult.append("File signature");
//						} else {
//							strResult.append(", signature");
//						}
//					} else {
//						Log.i("Upload Sign", "Successful!");
//					}
//				}
				
			}
		} catch (InterruptedException e) {
			Log.i("InterruptedException Data", e.getMessage());
		} catch (ExecutionException e) {
			Log.i("ExecutionException Data", e.getMessage());
		}
		return strResult.toString();
	}

	@Override
	protected void onPostExecute(String result) {
		mProgress.dismiss();
		mCtx.startActivity(new Intent(mCtx, Welcome.class).putExtra("result", result));
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		mProgress.setIcon(android.R.drawable.stat_sys_upload);
		mProgress.setTitle("Upload data");
		mProgress.setMessage("Uploading...");
		mProgress.setCancelable(false);
		mProgress.setCanceledOnTouchOutside(false);
		mProgress.show();
		super.onPreExecute();
	}
}
